package com.ruoyi.web.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.UserAnswerTable;
import com.ruoyi.system.service.IUserAnswerTableService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户答题管理Controller
 *
 * @author ruoyi
 * @date 2022-08-27
 */
@Controller
@RequestMapping("/system/table")
public class UserAnswerTableController extends BaseController {
    private String prefix = "system/table";

    @Autowired
    private IUserAnswerTableService userAnswerTableService;

    @RequiresPermissions("system:table:view")
    @GetMapping()
    public String table() {
        return prefix + "/table";
    }

    /**
     * 查询用户答题管理列表
     */
    @RequiresPermissions("system:table:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(UserAnswerTable userAnswerTable) {
        startPage();
        List<UserAnswerTable> list = userAnswerTableService.selectUserAnswerTableList(userAnswerTable);
        return getDataTable(list);
    }

    /**
     * 导出用户答题管理列表
     */
    @RequiresPermissions("system:table:export")
    @Log(title = "用户答题管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(UserAnswerTable userAnswerTable) {
        List<UserAnswerTable> list = userAnswerTableService.selectUserAnswerTableList(userAnswerTable);
        ExcelUtil<UserAnswerTable> util = new ExcelUtil<UserAnswerTable>(UserAnswerTable.class);
        return util.exportExcel(list, "用户答题管理数据");
    }

    /**
     * 新增用户答题管理
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存用户答题管理
     */
    @RequiresPermissions("system:table:add")
    @Log(title = "用户答题管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(UserAnswerTable userAnswerTable) {
        return toAjax(userAnswerTableService.insertUserAnswerTable(userAnswerTable));
    }

    /**
     * 修改用户答题管理
     */
    @RequiresPermissions("system:table:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        UserAnswerTable userAnswerTable = userAnswerTableService.selectUserAnswerTableById(id);
        mmap.put("userAnswerTable", userAnswerTable);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户答题管理
     */
    @RequiresPermissions("system:table:edit")
    @Log(title = "用户答题管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(UserAnswerTable userAnswerTable) {
        return toAjax(userAnswerTableService.updateUserAnswerTable(userAnswerTable));
    }

    /**
     * 删除用户答题管理
     */
    @RequiresPermissions("system:table:remove")
    @Log(title = "用户答题管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(userAnswerTableService.deleteUserAnswerTableByIds(ids));
    }

    /**
     * 获得用户列表，可以改成获得一个Int值
     * @param userAnswerTable
     * @return
     */
    @PostMapping("/getList")
    @ResponseBody
    public AjaxResult getList(UserAnswerTable userAnswerTable) {
        return success(userAnswerTableService.selectUserAnswerTableList(userAnswerTable));
    }
    @PostMapping("/additem")
    @ResponseBody
    public AjaxResult additem(UserAnswerTable userAnswerTable){
        UserAnswerTable temp=new UserAnswerTable();
        temp.setUserId(userAnswerTable.getUserId());
        temp.setTopicId(userAnswerTable.getTopicId());
        temp.setTopicType(userAnswerTable.getTopicType());
        List<UserAnswerTable> templist = userAnswerTableService.selectUserAnswerTableList(temp);
        if (templist.size()==0){
            return success(userAnswerTableService.insertUserAnswerTable(userAnswerTable));
        }else {
            Long id = templist.get(0).getId();
            userAnswerTableService.deleteUserAnswerTableById(id);
            return success(userAnswerTableService.insertUserAnswerTable(userAnswerTable));

        }

    }
}
